<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {hasEditor,buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				offCacheKey:"obj-jf-tpWaward-off-cache",
				itemCacheKey:"obj-jf-tpWaward-item-cache"
			})
		},
		methods:{
			getFlagByAwardTypeCode(id){
				let _that = this
				_that.om.request(
					"tpAward/getFlagByAwardTypeCode",
					{
						awardTypeId:id
					}
				).then(res=>{
					if(res.value.scoreDisabledFlag){
						_that.$refs.autoform.clear("title2","deductPoint")
						_that.$refs.autoform.changeEditor("title2","deductPoint","type","labelbox")
					}else{
						_that.$refs.autoform.changeEditor("title2","deductPoint","prompt","请输入")
						_that.$refs.autoform.changeEditor("title2","deductPoint","type","numberbox")
					}
					if(res.value.moneyDisabledFlag){
						_that.$refs.autoform.clear("title2","money")
						_that.$refs.autoform.changeEditor("title2","money","type","labelbox")
					}else{
						_that.$refs.autoform.changeEditor("title2","money","prompt","请输入")
						_that.$refs.autoform.changeEditor("title2","money","type","numberbox")
						
					}
				})
			},
			findPostList(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPosComboboxByUser",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title2","posId","data",configSelectData(res,'id','posname'))
					if(_that.$refs.autoform.hasEditor("title2","posId")){
						if(!_that.$refs.autoform.formdata.posId){
							_that.$refs.autoform.initSelectVal("title2","posId")
						}else{
							_that.$refs.autoform.initSelectIndex("title2","posId")
						}
					}
				})
			},
			clearOff(){
				this.$refs.autoform.clear("title2","awardId")
				this.$refs.autoform.clear("title2","posId")
			},
			clearItem(){
				this.$refs.autoform.clear("title2","itemId")
				this.$refs.autoform.clear("title2","deductPoint")
				this.$refs.autoform.clear("title2","money")
			},
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: _that.offCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							if(formdata.awardId != d.id){
								formdata.awardId = d.id
								formdata.awardName = d.username
								_that.$refs.autoform.clear("title2","posId")
								_that.findPostList(d.id)
								_that.$refs.autoform.clear("title2","item")
								_that.$refs.autoform.clear("title2","num")
								_that.$refs.autoform.clear("title2","deductPoint")
								_that.$refs.autoform.clear("title2","money")
							}
							uni.removeStorage({
								key: _that.offCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.itemCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.itemId = d.id
							formdata.itemName = d.itemName
							formdata.awardTypeId = d.awardTypeId
							formdata.awardTypeName = d.awardTypeName
							_that.getFlagByAwardTypeCode(d.awardTypeId)
							formdata.checkRemark = d.checkRemark
							formdata.remark = d.remark
							uni.removeStorage({
								key: _that.itemCacheKey
							});
						}
					}
				});
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								changeEditor("title2","awardId",{
									link:"../../public/pageSearch/pageSearch",
									"linkParam": {
										other:JSON.stringify({
											firstUserId:''
										}),
										namefield:'userPosName',
										extrafield: 'instName',
										searchfield:"q",
										initload:true,
										url:'temployee/getStaffCombogrid',
										cachename:_that.offCacheKey,
										placeholder:"请输入姓名或岗位"
									}
								},null,_that.autoformdata)
								changeEditor("title2","itemId",{
									link:"../../public/search/search",
									"linkParam": {
										namefield:'itemName',
										initload:true,
										url:'tpAward/getItemList',
										cachename:_that.itemCacheKey,
										placeholder:"请选择或输入"
									}
								},null,_that.autoformdata)

								// let year = moment(new Date()).format('YYYY');
								// let month = moment(new Date()).format('MM');
								// let year2 = '';
								// let month2 = '';
								// if(month == '01'){
								// 	month2 = '12'
								// 	year2 = (parseInt(year) - 1).toString();
								// }else{
								// 	year2 = year
								// 	month2 = (parseInt(month) - 1)<10 ? '0'+(parseInt(month) - 1) : (parseInt(month) - 1).toString();
								// }
								// let arr = [
								// 	{
								// 		value: year2+'-'+month2,
								// 		name: year2+'-'+month2,
								// 	},
								// 	{
								// 		value: year+'-'+month,
								// 		name: year+'-'+month,
								// 	},
								// ]
								// changeEditor('title2', 'month', 'data', configSelectData(arr, 'value', 'name'), _that.autoformdata)
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileSaveNameField", "fileSaveName")
								changeEditor(_that.autoformdata[0].editors, "fileList", "uploadUrl", "uploadFileData/uploadInterimFile")
								changeEditor(_that.autoformdata[0].editors, "fileList", "loadUrl", "uploadFileData/getInterimFile")
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", 45)
								if(_that.formdata.fileList) {
									var tempFileList = []
									var fileUrlList = _that.formdata.fileList.map(t=>{
										tempFileList.push(t.fileSaveName)
										return t.fileSaveName
									})
									changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
									changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
								}
								_that.$nextTick(function(){
									// _that.$refs.autoform.initSelectIndex("title2", "month")
									if(_that.$refs.autoform.formdata.awardId){
										_that.findPostList(_that.$refs.autoform.formdata.awardId)
									}
									if(_that.$refs.autoform.formdata.awardTypeId){
										_that.getFlagByAwardTypeCode(_that.$refs.autoform.formdata.awardTypeId)
									}
								})
							}
						}
					})
				}					
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
